package com.CampusLife.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.IOException;

@Controller
public class Index {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @RequestMapping("/websocket/{name}")
    public String webSocket(@PathVariable String name, Model model){
        try{
            logger.info("跳转到websocket的页面上");
            model.addAttribute("username",name);
            return "index";
        }
        catch (Exception e){
            logger.info("跳转到websocket的页面上发生异常，异常信息是："+e.getMessage());
            return "";
        }
    }

    @PostMapping("/websocket/questionType/importExcel")
    @ResponseBody
    public String uploadExportWord(MultipartFile files,@RequestParam("username")String username,@RequestParam("selectText")String selectText) {
        String fileName = files.getOriginalFilename();
        fileName=KeyUtil.genUniqueKey()+fileName;
        String filePath=ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/imgupload/";
        System.out.println(filePath);
        //文件上传
        try {
            FileUtil.uploadFile(files.getBytes(), filePath, fileName);
            WebSocket webSocket=new WebSocket();
            String a="{\"message\":\"/imgupload/"+fileName+"\",\"username\":\""+username+"\",\"to\":\""+selectText+"\",\"type\":1}";
            webSocket.onMessage(a,null);
        }catch (Exception e){
            e.printStackTrace();
        }
        String base64="";
        try {
            base64="data:image/jpeg;base64,"+Base64.encodeBase64String(files.getBytes());
        }catch (Exception e){
            e.printStackTrace();
        }

        return base64;
    }

    @RequestMapping("/websocket/questionType/importAudio")
    @ResponseBody
    public void importAudio(@RequestParam("dataurl") String dataurl,@RequestParam("username")String username,@RequestParam("selectText")String selectText) {
        dataurl = dataurl.replaceAll("data:audio/mp3;base64,","");
        byte[] audioByte=Base64.decodeBase64(dataurl);
        String fileName=KeyUtil.genUniqueKey()+".mp3";
        String filePath=ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/audioupload/";
        //文件上传
        try {
            FileUtil.uploadFile(audioByte, filePath, fileName);
            WebSocket webSocket=new WebSocket();
            String a="{\"message\":\"/audioupload/"+fileName+"\",\"username\":\""+username+"\",\"to\":\""+selectText+"\",\"type\":2}";
            webSocket.onMessage(a,null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
